﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Binding基础
{
    /// <summary>
    /// binding校验.xaml 的交互逻辑
    /// </summary>
    public partial class binding校验 : Window
    {
        public binding校验()
        {
            InitializeComponent();





            //Binding binding = new Binding("Value") { Source = slider };
            //binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            //RangValidRule rvr = new RangValidRule();
            //rvr.ValidatesOnTargetUpdated = true;
            //binding.ValidationRules.Add(rvr);

            //binding.NotifyOnValidationError = true;

            //binding.Mode= BindingMode.
            //this.txBox.SetBinding(TextBox.TextProperty, binding);

            //this.txBox.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
            Convert.ToBoolean();
        }

        //void ValidationError(object sender, RoutedEventArgs e)
        //{
        //    if (Validation.GetErrors(this.txBox).Count > 0)
        //    {
        //        this.txBox.ToolTip = Validation.GetErrors(this.txBox)[0].ErrorContent.ToString();
        //    }
        //}
    }


    public class StringToBool : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return System.Convert.ToBoolean(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ValueConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class RangValidRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (double.TryParse(value.ToString(), out double b))
            {
                //输入的值在[20,50]为正确值
                if (b >= 00 && b <= 50)
                {
                    return new ValidationResult(true, null);
                }
            }
            return new ValidationResult(false, "Valid Failed");
        }
    }
}
